package com.informationsystem.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.informationsystem.service.AuthorityManager;
import com.informationsystem.service.SystemuserManager;
import com.informationsystem.dao.SearchException;
import com.informationsystem.model.Authority;
import com.informationsystem.model.Systemuser;
import com.informationsystem.webapp.action.BaseAction;

import java.util.ArrayList;
import java.util.List;

public class SystemuserAction extends BaseAction implements Preparable {
    private AuthorityManager authorityManager;
    private List systemusers;
    private Systemuser systemuser;
    private Long id;
    private String query;
    private Authority authority;


    public void setSystemuserManager(SystemuserManager systemuserManager) {
        this.systemuserManager = systemuserManager;
    }

    public List getSystemusers() {
        return systemusers;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String systemuserId = getRequest().getParameter("systemuser.id");
            if (systemuserId != null && !systemuserId.equals("")) {
                systemuser = systemuserManager.get(new Long(systemuserId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            systemusers = systemuserManager.search(query, Systemuser.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            systemusers = systemuserManager.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Systemuser getSystemuser() {
        return systemuser;
    }

    public void setSystemuser(Systemuser systemuser) {
        this.systemuser = systemuser;
    }

    public String delete() {
    	systemuser = systemuserManager.get(systemuser.getId());
        systemuserManager.remove(systemuser.getId());
        addLog("remove user id:" + systemuser.getId() + " username:" + systemuser.getUsername());
        saveMessage(getText("systemuser.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            systemuser = systemuserManager.get(id);
        } else {
            systemuser = new Systemuser();
        }

        return SUCCESS;
    }

    public String addSystemUserAuthority() throws Exception{
    	boolean isNew = (systemuser.getId() == null);
    	   if (!isNew) {
        	   systemuser = systemuserManager.get(systemuser.getId());
        	   authority = authorityManager.get(authority.getId());
        	   systemuser.getAuthorities().add(authority);
        	   addLog("add authority name:" + authority.getName() + " to username:" + systemuser.getUsername());
           } else {
               return INPUT;
           }
    	   systemuserManager.save(systemuser);
    	   return SUCCESS;
    }
    
    public String deleteSystemUserAuthority() throws Exception{
    	boolean isNew = (systemuser.getId() == null);
    	   if (!isNew) {
        	   systemuser = systemuserManager.get(systemuser.getId());
        	   authority = authorityManager.get(authority.getId());
        	   systemuser.getAuthorities().remove(authority);
        	   addLog("remove authority name:" + authority.getName() + " from username:" + systemuser.getUsername());
           } else {
               return INPUT;
           }
    	   systemuserManager.save(systemuser);
    	   return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (systemuser.getId() == null);

        systemuser = systemuserManager.save(systemuser);

        String key = (isNew) ? "systemuser.added" : "systemuser.updated";
        saveMessage(getText(key));
        
        if(!isNew){
        	addLog("edit user id:" + systemuser.getId() + " username:" + systemuser.getUsername());
        }else{
        	addLog("add user id:" + systemuser.getId() + " username:" + systemuser.getUsername());
        }
        
        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

	public Authority getAuthority() {
		return authority;
	}

	public void setAuthority(Authority authority) {
		this.authority = authority;
	}

	public AuthorityManager getAuthorityManager() {
		return authorityManager;
	}

	public void setAuthorityManager(AuthorityManager authorityManager) {
		this.authorityManager = authorityManager;
	}
	public List getAuthorityList(){
		return authorityManager.getAll();
	}
	
	public List getUserTypeList(){
	    List userType = new ArrayList();
	    userType.add("User");
	    userType.add("Admin");
	    return userType;
	    
	}
}